package com.example.dqhl.recycleviewdemo.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.example.dqhl.recycleviewdemo.R;
import com.example.dqhl.recycleviewdemo.activity.eventbus.EventBusTestActivity;
import com.example.dqhl.recycleviewdemo.base.BaseHandler;
import com.example.dqhl.recycleviewdemo.util.PermissionsChecker;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class LoginActivity extends AppCompatActivity implements BaseHandler.HandleMessage {

    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    @BindView(R.id.animation_btn)
    Button coordinationBtn;
    @BindView(R.id.customView_btn)
    Button customViewBtn;
    @BindView(R.id.horzienScroll_btn)
    Button horzienScrollBtn;
    @BindView(R.id.webview_btn)
    Button webviewBtn;
    @BindView(R.id.recycleView_btn)
    Button recycleViewBtn;
    @BindView(R.id.textView_btn)
    Button textViewBtn;
    @BindView(R.id.mkLoader_btn)
    Button mkLoaderBtn;
    @BindView(R.id.parallaxRecycle_btn)
    Button parallaxRecycleBtn;
    @BindView(R.id.recycleStraggedGrid_btn)
    Button recycleStraggedGridBtn;
    @BindView(R.id.surfaceView_btn)
    Button surfaceViewBtn;
    @BindView(R.id.tabLayout_btn)
    Button tabLayoutBtn;
    @BindView(R.id.cursor_btn)
    Button voiceViewBtn;
    @BindView(R.id.bluetooth_btn)
    Button bluetoothBtn;
    @BindView(R.id.eventbus_btn)
    Button eventbusBtn;
    @BindView(R.id.retrofit_btn)
    Button retrofitBtn;
    @BindView(R.id.rxjava_btn)
    Button rxjavaBtn;


    private PermissionsChecker mPermissionsChecker; // 权限检测器
    private static final int PERMISSION_REQUEST_CODE = 0; // 系统权限管理页面的参数

    private static final int REFRESH_COMPLETE = 0X153;

    private Handler handler = new BaseHandler<>(LoginActivity.this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        mPermissionsChecker = new PermissionsChecker(this);
        initPermission();

    }

    private void initPermission() {
        if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
            if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
                //请求权限
                ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_REQUEST_CODE);
            } else {
                initData();
//                startAnim(); // 全部权限都已获取
            }
        } else {
//            startAnim(); // 全部权限都已获取
        }
    }

    private void initData() {
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_REQUEST_CODE && hasAllPermissionsGranted(grantResults)) {
//            startAnim();
        } else {
            handler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 1000);
        }
    }

    /**
     * 含有全部的权限
     *
     * @param grantResults
     * @return
     */
    private boolean hasAllPermissionsGranted(@NonNull int[] grantResults) {
        for (int grantResult : grantResults) {
            if (grantResult == PackageManager.PERMISSION_DENIED) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case REFRESH_COMPLETE:
                finish();
                break;
        }
    }

    Intent intent = new Intent();

    @OnClick({R.id.animation_btn,  R.id.customView_btn, R.id.horzienScroll_btn,
            R.id.webview_btn, R.id.recycleView_btn, R.id.textView_btn, R.id.mkLoader_btn,
            R.id.parallaxRecycle_btn, R.id.recycleStraggedGrid_btn, R.id.surfaceView_btn,
            R.id.tabLayout_btn, R.id.cursor_btn,R.id.bluetooth_btn, R.id.eventbus_btn
            , R.id.retrofit_btn, R.id.rxjava_btn, R.id.time_btn,R.id.click_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.animation_btn:
                intent.setClass(LoginActivity.this, AnimationActivity.class);
                startActivity(intent);
                break;
            case R.id.customView_btn:
                intent.setClass(LoginActivity.this, CustomViewActivity.class);
                startActivity(intent);
                break;
            case R.id.horzienScroll_btn:
                intent.setClass(LoginActivity.this, HorizenScrollerActivity.class);
                startActivity(intent);
                break;
            case R.id.webview_btn:
                intent.setClass(LoginActivity.this, WebViewActivity.class);
                startActivity(intent);
                break;
            case R.id.recycleView_btn:
                intent.setClass(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                break;
            case R.id.textView_btn:
                intent.setClass(LoginActivity.this, MoreTextViewActivity.class);
                startActivity(intent);
                break;
            case R.id.mkLoader_btn:
                intent.setClass(LoginActivity.this, MkLoaderActivity.class);
                startActivity(intent);
                break;
            case R.id.parallaxRecycle_btn:
                intent.setClass(LoginActivity.this, ParallaxRecycleActivity.class);
                startActivity(intent);
                break;
            case R.id.recycleStraggedGrid_btn:
                intent.setClass(LoginActivity.this, StraggedGridActivity.class);
                startActivity(intent);
                break;
            case R.id.surfaceView_btn:
                intent.setClass(LoginActivity.this, SurfaceViewActivity.class);
                startActivity(intent);
                break;
            case R.id.tabLayout_btn:
                intent.setClass(LoginActivity.this, TabLayoutActivity.class);
                startActivity(intent);
                break;
            case R.id.cursor_btn:
                intent.setClass(LoginActivity.this, CoursorWheelActivity.class);
                startActivity(intent);
                break;
            case R.id.bluetooth_btn:
                intent.setClass(LoginActivity.this, BluetoothActivity.class);
                startActivity(intent);
                break;
            case R.id.eventbus_btn:
                intent.setClass(LoginActivity.this, EventBusTestActivity.class);
                startActivity(intent);
                break;
            case R.id.retrofit_btn:
                intent.setClass(LoginActivity.this, RetrofitActivity.class);
                startActivity(intent);
                break;
            case R.id.rxjava_btn:
                intent.setClass(LoginActivity.this, RxJavaActivity.class);
                startActivity(intent);
                break;
            case R.id.time_btn:
                intent.setClass(LoginActivity.this, TimeActivity.class);
                startActivity(intent);
                break;
            case R.id.click_btn:
                break;
            default:break;
        }
    }
}
